<?php
/**
 * Created by Alan.
 * User: Alan
 * Date: 2018\10\9 0009
 * Time: 8:54
 */

namespace app\admin\controller;
use app\common\model\Bis as BisModel;

use app\common\model\Category;
use app\common\model\City;
use app\common\validate\BisValidate;
use think\Controller;

class Bis extends Controller
{
    //商家列表
    public function index()
    {
        $bis = (new BisModel())->getBisByStatus(1);
        return $this->fetch('index',[
            'bis' => $bis
        ]);
    }
    //商家审核
    public function apply()
    {
        $bisModel = new BisModel();
        $bis = $bisModel->getBisByStatus();
        return $this->fetch('apply',[
            'bis' => $bis
        ]);
    }

    //商户详情
    public function detail()
    {
        $id = input('get.id');
        if(empty($id)) {
            return $this->error('ID错误');
        }
        //获取一级城市的数据
        $citys = City::getNormalCityByParentId();
        //获取一级栏目的数据
        $categorys = Category::getNomalCategoryByParentId();
        // 获取商户数据
        $bisData = model('Bis')->get($id);

        $locationData = model('BisLocation')->get(['bis_id'=>$id, 'is_main'=>1]);
        $accountData = model('BisAccount')->get(['bis_id'=>$id, 'is_main'=>1]);
        return $this->fetch('',[
            'citys' => $citys,
            'categorys' => $categorys,
            'bisData' => $bisData,
            'locationData' => $locationData,
            'accountData' => $accountData,
        ]);
    }

    //删除的商家列表
    public function dellist()
    {
        $bis = (new BisModel())->getBisByStatus(-1);
        return $this->fetch('dellist',[
            'bis' => $bis
        ]);
    }

    //状态修改
    public function status()
    {
        $validate = (new BisValidate())->goCheck('status');
        if($validate !== true){
            $this->error($validate);
        }

        $data = input('get.');
        $res = model('Bis')->save(['status' => $data['status']],['id' => $data['id']]);
        $location = model('BisLocation')->save(['status'=> $data['status']],['bis_id' => $data['id'],'is_main' => 1]);
        $account = model('BisAccount')->save(['status' => $data['status']],['bis_id' => $data['id'],'is_main' => 1]);

        if($res && $location && $account){
            //发送邮件
//            \phpmailer\Email::send($data['email'],$title,$content);
            $this->success('状态更新成功！');
        }else {
            $this->error('状态更新失败！');
        }
    }
}